# +----------------------------------------------------------------------
# | ChatWork智能聊天办公系统
# +----------------------------------------------------------------------
# | 软件声明: 本系统并非自由软件,未经授权任何形式的商业使用均属非法。
# | 版权保护: 任何企业和个人不允许对程序代码以任何形式任何目的复制/分发。
# | 授权要求: 如有商业使用需求,请务必先与版权所有者取得联系并获得正式授权。
# +----------------------------------------------------------------------
# | Author: ChatWork Team <2474369941@qq.com>
# +----------------------------------------------------------------------
import time
from tortoise.queryset import Q
from hypertext import PagingResult
from common.enums.notice import NoticeEnum
from common.models.notice import NoticeRecord
from apps.api.schemas import notice_schema as schema


class NoticeService:
    """ 消息通知服务类 """

    @classmethod
    async def lists(cls, user_id: int, params: schema.NoticeSearchIn) -> PagingResult[schema.NoticeListVo]:
        """
        消息列表

        Args:
            user_id (int): 用户ID
            params (schema.NoticeSearchIn): 筛选参数

        Returns:
            PagingResult[schema.NoticeListVo]

        Author:
            zero
        """
        where = [
            Q(user_id=user_id),
            Q(is_delete=0),
            Q(receiver=1),
            Q(sender=NoticeEnum.SENDER_SYS)
        ]

        _model = NoticeRecord.filter(*where).order_by("-id")
        _pager = await NoticeRecord.paginate(
            model=_model,
            page_no=params.page_no,
            page_size=params.page_size,
            fields=["scene", "title", "content", "is_read", "create_time"]
        )

        _lists = []
        for item in _pager.lists:
            _lists.append(schema.NoticeListVo(
                scene=item["scene"],
                title=item["title"],
                content=item["content"],
                is_read=item["is_read"],
                create_time=item["create_time"]
            ))

        _pager.lists = _lists
        return _pager

    @classmethod
    async def read(cls, user_id: int, id_: int):
        """
        标记已读

        Args:
            user_id (int): 用户ID
            id_ (int): 消息ID

        Author:
            zero
        """
        where = []
        if id_ > 0:
            where.append(Q(id=id_))

        await (NoticeRecord
               .filter(*where)
               .filter(receiver=1)
               .filter(sender=NoticeEnum.SENDER_SYS)
               .filter(user_id=user_id)
               .filter(is_delete=0)
               .filter(is_read=0)
               .update(is_read=1, update_time=int(time.time())))

    @classmethod
    async def delete(cls, user_id: int, id_: int):
        """
        消息删除

        Args:
            user_id (int): 用户ID
            id_ (int): 消息ID

        Author:
            zero
        """
        where = []
        if id_ > 0:
            where.append(Q(id=id_))

        await (NoticeRecord
               .filter(*where)
               .filter(sender=NoticeEnum.SENDER_SYS)
               .filter(user_id=user_id)
               .filter(is_delete=0)
               .filter(is_read=1)
               .filter(receiver=1)
               .update(is_delete=1, delete_time=int(time.time())))
